package demo;

import java.util.Arrays;
import java.util.Comparator;

class Student implements Comparable {
    private String name;
    private int score;

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Student(String name, int score) {
        this.name = name;
        this.score = score;
    }


    @Override
    public String toString() {
        return "student{" +
                "name='" + name + '\'' +
                ", score=" + score +
                '}';
    }

    //@Override
//    public int compareTo(Object o) {
//        Student s = (Student)o;//向下转型
//        if(this.score > s.score){
//            return -1;
//        }else if(this.score < s.score){
//            return 1;
//        }else{
//            return 0;
//        }
//    }

    @Override
    public int compareTo(Object o) {
        Student s = (Student) o;
        return this.name.compareTo(s.name);
    }
}



public class Test {
    public static void main(String[] args) {
        Student[] students = new Student[]{new Student("张三",90),
                new Student("李四",98),new Student("王五",82)};
//        ScoreComparator scoreComparator = new ScoreComparator();
//        Arrays.sort(students,scoreComparator);
        bubbleSort(students);
        System.out.println(Arrays.toString(students));
    }

    public static void bubbleSort(Comparable[] array){
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = 0; j < array.length - i - 1; j++) {
                if(array[j].compareTo(array[j + 1]) > 0){
                    Comparable temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
    }
}


//构造器的构建
class ScoreComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.getScore() - o2.getScore();
    }
}

class NameCOmparator implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return o1.getName().compareTo(o2.getName());
    }
}